import Vue from "vue";
import VueRouter from "vue-router";

import Login from "@/components/Login";
import Home from '@/components/Home'
import Welcome from '@/components/Welcome'
import User from '@/components/users/User'
import Permiss from '@/components/permiss/Permiss'
import Roles from '@/components/roles/Roles'
import Cate from '@/components/goods/Cate'
import Params from '@/components/goods/Params'
import List from '@/components/goods/List'
import Add from '@/components/goods/Add'
import Orders from '@/components/orders/Orders'
Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/login" },
  { path: "/login", component: Login },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: Welcome },
      { path: "/users", component: User },
      { path: "/rights", component: Permiss },
      { path: "/roles", component: Roles },
      { path: "/categories", component: Cate },
      { path: "/params", component: Params },
      { path: "/goods", component: List },
      { path: "/goods/add", component: Add },
      { path: "/orders", component: Orders },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
      if(to.path !== '/login'){
         const getToken = sessionStorage.getItem('token')
         if(getToken){
          next()
         }else{
           next('/login')
         }
      }else{
        next()
      }
})
export default router;